package com.woniuxy.service.impl;

import com.woniuxy.entity.User;
import com.woniuxy.mapper.UserMapper;
import com.woniuxy.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Override
    public List<User> queryAll() {
        List<User> users = userMapper.queryAll();
        return users;
    }

    @Override
    public User queryById(Integer id) {
        User user = userMapper.queryById(id);
        return user;
    }

    @Override
    public List<User> queryByCondition(String name, String gender) {
        List<User> users = userMapper.queryByCondition(name, gender);
        return users;
    }

    @Override
    public Integer add(User user) {
        Integer add = userMapper.add(user);
        return add;
    }

    @Override
    public Integer delete(Integer id) {
        Integer delete = userMapper.delete(id);
        return delete;
    }

    @Override
    public Integer updateByIdSelective(User user) {
        Integer result = userMapper.updateByIdSelective(user);
        return result;
    }

    @Override
    public Map<String, Object> login(User user) {
        HashMap<String, Object> map = new HashMap<>();
        String userName = user.getName();
        User user1 = userMapper.queryByName(user);
        if (user1 == null) {
            map.put("code",500);
            map.put("msg","找不到用户");
        } else if(userName.equals(user1.getName())){
            if (!user.getPassword().equals(user1.getPassword())) {
                map.put("code",500);
                map.put("msg","密码错误");
            } else {
                map.put("code",200);
                map.put("msg","登录成功");
                map.put("data",user1);
            }
        }
        return map;
    }
}
